package com.wk.springboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
@MapperScan(basePackages = {"com.thunisoft.qbmh.mapper.dm"}, sqlSessionTemplateRef = "DmSqlSessionTemplate")
public class MybatisDmConfig {


    @Bean(name = "DmDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.dm")
    public DataSource primaryDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "DmSqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory1(@Qualifier("DmDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        return factoryBean.getObject();
    }

    @Bean(name = "DmSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("DmSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
